<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <!--添加面经-->
          <el-button
            @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>
      <!--面经数据表格-->
      <el-table :data="list" border height="500" style="width: 100%">
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="{ $index }">
            {{ obj.pageSize * (obj.current - 1) + $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column align="left" prop="stem" label="标题" width="300">
        </el-table-column>
        <el-table-column align="center" prop="creator" label="作者" width="120">
        </el-table-column>
        <el-table-column align="center" prop="likeCount" label="点赞">
        </el-table-column>
        <el-table-column align="center" prop="views" label="浏览数" width="120">
        </el-table-column>
        <el-table-column align="center" prop="createdAt" label="更新时间">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <div class="actions">
              <i
                class="el-icon-view"
                @click="openDrawer('preview', row.id)"
              ></i>
              <i
                class="el-icon-edit-outline"
                @click="openDrawer('edit', row.id)"
              ></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="obj.current"
        :page-sizes="listarr"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!--表单结构-->
      <el-drawer :title="title" :visible.sync="drawer">
        <el-form
          label-width="80px"
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
        >
          <el-form-item label="标题" prop="stem">
            <el-input
              v-model="ruleForm.stem"
              placeholder="输入面经标题"
            ></el-input>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <quillEditor v-model="ruleForm.content"></quillEditor>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="add">确认</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>

import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { getArticleList, removeArticle, createArticle } from '@/api/article.js'
export default {
  name: 'article-page',
  components: { quillEditor },
  data () {
    return {
      total: 0,
      list: [],
      obj: {
        current: 1,
        pageSize: 10
      },
      listarr: [10, 20, 30, 50],
      drawer: false,
      title: "",
      ruleForm: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入面经标题', trigger: 'blur' }]
      }

    }
  },
  created () {
    this.getdataList()
  },
  methods: {
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.obj.pageSize = val
      this.getdataList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.obj.current = val
      this.getdataList()
    },
    handleClick (row) {
      console.log(row);
    },
    //获取面经数据
    async getdataList () {
      const res = await getArticleList(this.obj)
      console.log(res, 999998);
      this.list = res.data.rows
      this.total = res.data.total
    },
    //删除面经
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await removeArticle(id)
        this.$message({
          type: 'success',
          message: res.data
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    //添加面经
    openDrawer (type) {
      if (type == "add") {
        this.title = '添加文章'
      } else if (type == "edit") {
        this.title = '编辑文章'
      } else {
        this.title = '查看文章'
      }
      this.drawer = true
    },
    //添加面经
    async add () {
      console.log(this.title);
      if (this.title == '添加文章') {
        const res = await createArticle(this.ruleForm)
        //添加之后,回到第一页
        this.obj.current = 1
        this.getdataList()
        console.log(res);
        //清空表单
        this.$refs['ruleForm'].resetFields()
        this.drawer = false
      }

    }

  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>